/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package locusts.client;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import locusts.common.GameConfig;
import locusts.common.Player;
import locusts.common.WorldModel;

/**
 *
 * @author hamish
 */
public class RenderPanel extends JPanel {

    // move to RenderConfig
    //
    private WorldModel world;
    private GameConfig gConfig;
    private RenderConfig rConfig;

    public RenderPanel(RenderConfig rConfig) {
        setSize(400, 500);
        setVisible(true);
        this.rConfig = rConfig;
        world = null;
        gConfig = null;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        this.render(g2);
    }

    public void setGameConfig(GameConfig cfg) {
        this.gConfig = cfg;
    }

    public void setWorld(WorldModel world) {
        this.world = world;
    }

    public void render(Graphics2D g) {
        if (world == null || gConfig == null)
            return;
        g.setRenderingHints(rConfig.getRenderingHints());
        for (Player p : world.getPlayers()) {
            renderPlayer(g, p);
        }
    }

    private void renderPlayer(Graphics2D g, Player p) {
        if (world.isIt(p)) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.BLUE);
        }
        int x = (int) (p.getX() - gConfig.getTagRadius());
        int y = (int) (p.getY() - gConfig.getTagRadius());
        int d = (int) (2 * gConfig.getTagRadius());
        g.fillOval(x, y, d, d);
        g.setColor(Color.BLACK);
        g.drawString(p.getName(), x, y + d + 15);
    }
}
